let redis = require('../dao/redis');
let gameDao = require('../dao/gameDao');
let userDao = require('../dao/userDao');
let constants = require('../iconst');
let redisConst = require('../const/redisConst');
let assert = require('assert');
let iconst = require('../iconst');
let numUtil = require('../util/numUtil');
let apollo = require('../config/apollo');
let axi = require('axios');
let roomCacheService = require('./RoomCacheService');
const { BusiError, SysError } = require('../exception/exception');

let axios = null;
async function getAxios(){
    if(!axios){
        let conf = await apollo.getConfig();
        
        const config = {
            timeout: 1500,
            baseURL: conf["store.url"]
        };

        axios = axi.create(config);
    }

    return axios;
}

class StoreService {
    constructor() {

    }

    async saveRoomInfo(data){
        let res = await (await getAxios()).post("/api/saveRoomInfo", data);
        return res.data;
    }

    async saveSettlement(data){
        let res = await (await getAxios()).post("/api/saveSettlement", data);
        return res.data;
    }

    async querySettlement(data){
        let res = await (await getAxios()).post("/api/querySettlement", data);
        return res.data;
    }

    async saveSettlementItem(data){
        let res = await (await getAxios()).post("/api/saveSettlementItem", data);
        return res.data;
    }
    
    async querySettlementItem(uid, gameType){
        let res = await (await getAxios()).post("/api/querySettlementItem", {uid: uid, gameType:gameType});
        return res.data;
    }

    async querySettlementItemWithID(id){
        let res = await (await getAxios()).post("/api/querySettlementItemWithID", {id: id});
        return res.data;
    }

    //add
    async querySettlementItemWithClub(uid,clubid){
        let res = await (await getAxios()).post("/api/querySettlementItemWithClub", {uid: uid, clubid:clubid});
        return res.data;
    }

    //代理查询
    //查询本人
    async queryAgentWithClubMy(uid,clubid,tsdate){
        let res = await (await getAxios()).post("/api/queryAgentWithClubMy", {uid: uid, clubid:clubid, tsdate:tsdate});
        return res.data;
    }
    //查询下级
    async queryAgentWithClubLower(uid,clubid,tsdate){
        let res = await (await getAxios()).post("/api/queryAgentWithClubLower", {uid: uid, clubid:clubid, tsdate:tsdate});
        return res.data;
    }

    //查询详细
    async queryAgentWithClubDetailed(clubid,paiJuNo){
        let res = await (await getAxios()).post("/api/queryAgentWithClubDetailed", {clubid:clubid, paiJuNo:paiJuNo});
        return res.data;
    }

    async queryMemberSettlementItem(uid){
        //查找俱乐部战绩和大厅战绩
        let res = await (await getAxios()).post("/api/queryMemberSettlementItem", {uid: uid});
        //查找大联盟的战绩
        let res1 = await this.queryMemberUnionSettlementItem(uid);
        for(let i in res1){
            res.data.push(res1[i]);
        }
        res.data.sort(function(a, b){return b.roomTs - a.roomTs;});
        return res.data;
    }

    async querySettlementItemWithClubDetails(uid,roomid,roomts){
        console.log('发来消息下一步',roomid,roomts);
        let res = await (await getAxios()).post("/api/querySettlementItemWithClubDetails", {uid: uid,roomid: roomid, roomts:roomts});
        return res.data;
    }

    //大联盟
    async queryUnion(unionid,tsdate){
        console.log('发来消息下一步',unionid,tsdate);
        let res = await (await getAxios()).post("/api/queryDayScoreChief", {unionid: unionid,tsdate: tsdate});
        return res.data;
    }

    async queryUnionDetails(unionid,tsdate){
        console.log('发来消息下一步',unionid,tsdate);
        let res = await (await getAxios()).post("/api/querySettlementItemChiefWithUnion", {unionid: unionid,tsdate: tsdate});
        return res.data;
    }

    async queryUnionPlay(uid,unionid,tsdate){
        console.log('发来消息下一步',uid,unionid,tsdate);
        let res = await (await getAxios()).post("/api/queryDayScorePlayer", {uid: uid,unionid: unionid,tsdate: tsdate});
        return res.data;
    }

    async queryUnionPlayDetails(uid,unionid,tsdate){
        console.log('发来消息下一步',uid,unionid,tsdate);
        let res = await (await getAxios()).post("/api/querySettlementItemPlayerWithUnion", {uid: uid,unionid: unionid,tsdate: tsdate});
        return res.data;
    }

    async queryMemberUnionSettlementItem(uid){
        let res = await (await getAxios()).post("/api/queryMemberUnionSettlementItem", {uid: uid});
        return res.data;
    }

    async queryUnionRoomDetails(roomid,roomts){
        console.log('发来消息下一步',roomid,roomts);
        let res = await (await getAxios()).post("/api/querySettlementItemPlayerWithUnionDetails", {roomid: roomid,roomts: roomts});
        return res.data;
    }

    //////////////////

    async saveGameRecord(data){
        var    s=data;

        let res = await (await getAxios()).post("/api/saveGameRecord", data);
        if(typeof s=="string")
        {
            try{
                s=JSON.parse(data);


            }catch(e)
            {
                console.logg("saveGameRecord ",e.message)
            }

        }
        if(s)
        {
            var  roomId=s.roomID;
            await roomCacheService.updateGamingTick(roomId)
        }
        return res.data;
    }

    //大联盟
    async saveGameUnionRecord(data){
        let res = await (await getAxios()).post("/api/saveGameRecord/union", data);
        var  s=data;
        if(typeof s=="string")
        {
            try{
                s=JSON.parse(data);


            }catch(e)
            {
                console.logg("saveGameRecord ",e.message)
            }

        }
        if(s)
        {
            var  roomId=s.roomID;
            await roomCacheService.updateGamingTick(roomId)
        }

        return res.data;
    }

    async saveRoomRecord(data){
        let res = await (await getAxios()).post("/api/saveRoomRecord", data);
        // var  s=data;
        // if(typeof s=="string")
        // {
        //     try{
        //         s=JSON.parse(data);
        //
        //
        //     }catch(e)
        //     {
        //         console.logg("saveGameRecord ",e.message)
        //     }
        //
        // }
        // if(s)
        // {
        //     var  roomId=s.roomID;
        //     await roomCacheService.updateGamingTick(roomId)
        // }

        return res.data;
    }

    async saveRoomUnionRecord(data){
        //存储大赢家战绩
        let res = await (await getAxios()).post("/api/saveRoomRecord/union", data);
        return res.data;
    }

    async queryGameRecord(data){
        let res = await (await getAxios()).post("/api/queryGameRecord", data);
        return res.data;
    }
}

let storeService = new StoreService();
module.exports = storeService;